/*

File name		: table3.do

Description		: correlates donation rates with weather conditions									

Output File		: table3.tex
				
*/

set more off, perm
capture restore

*** 001 Summarising/Tabulation ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"
	*, keep(match)

	*rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort spnr entnahmedatum
	gen any_rain_plus1 = any_rain[_n+1] if spnr == spnr[_n+1]

	sort spnr entnahmedatum
	gen rainfall_plus1 = daily_rainfall[_n+1] if spnr == spnr[_n+1]
	gen strong_rain = 1 if daily_rainfall >= 10
	replace strong_rain = 0 if daily_rainfall < 10

	// Robustnesss: Extreme heat and cold. 
	gen extreme_cold = 1 if daily_high_ZH < 5 & daily_high_ZH != .
	replace extreme_cold = 0 if daily_high_ZH >= 5 & daily_high_ZH != .

	replace daily_low_ZH = "." if daily_low_ZH == "-"
	destring daily_low_ZH, replace
	gen frost_day = 1 if daily_low_ZH < 0 & daily_low_ZH != .
	replace frost_day = 0 if daily_low_ZH >= 0 & daily_low_ZH != .

	gen extreme_heat = 1 if daily_high_ZH > 25 & daily_high_ZH != .
	replace extreme_heat = 0 if daily_high_ZH <= 25 & daily_high_ZH != .

	gen adverse_weather = 0 if extreme_cold != .
	replace adverse_weather = 1 if extreme_cold == 1 | extreme_heat == 1 | strong_rain == 1

	// Summary Statistics
	lab var don "Donation rate"
	balancetable (mean if strong_rain == 0 & period < 4) (mean if strong_rain == 1 & period < 4) ///
	(mean if extreme_cold == 0 & period < 4) (mean if extreme_cold == 1 & period < 4) ///
	(mean if extreme_heat == 0 & period < 4) (mean if extreme_heat == 1 & period < 4) ///
	(mean if adverse_weather == 0 & period < 4) (mean if adverse_weather == 1 & period < 4) ///
	don using "output/tables/table3.tex", ///
	replace ctitles("No" "Yes" "No" "Yes" "No" "Yes" "No" "Yes") ///
	groups("Rain" "Cold" "Heat" "Any of the three", pattern(1 0 1 0 1 0 1 0) ///
	end("\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9}")) ///
	varlabels nonumbers
	
	// Additional manually added stats
	foreach j of varlist strong_rain extreme_cold extreme_heat adverse_weather{
	// No of blood drives
	codebook c_num if `j'==0 & period<4
	codebook c_num if `j'==1 & period<4
	// No of days
	codebook entnahmedatum if `j'==0 & period<4
	codebook entnahmedatum if `j'==1 & period<4
	}

